var t = null;

Page({
    data: {
        modalName: "",
        seconds: 5,
        time: 0,
        running: !1,
        players: []
    },
    showModal: function(t) {
        var a = Number(t.currentTarget.dataset.type), e = 0 == a ? [] : this.data.players, s = 0 == a ? 0 : this.data.time;
        this.setData({
            modalName: t.currentTarget.dataset.target,
            players: e,
            time: s
        });
    },
    hideModal: function() {
        this.setData({
            modalName: null
        });
    },
    secondsMinus: function() {
        this.setData({
            seconds: this.data.seconds - 1
        });
    },
    secondsAdd: function() {
        this.setData({
            seconds: this.data.seconds + 1
        });
    },
    start: function() {
        if (null == t) {
            var a = this;
            a.setData({
                time: 0,
                running: !0
            });
            var e = [ 71, 73, 81, 77, 79, 69, 67 ], s = Math.floor(Math.random() * e.length);
            t = setInterval(function() {
                a.setData({
                    time: a.data.time + e[s]
                });
            }, e[s]);
        }
    },
    end: function() {
        if (null != t) {
            clearInterval(t), t = null;
            var a = this.data.players, e = (this.data.time - 1e3 * this.data.seconds) / 1e3;
            a.push({
                name: this.data.players.length + 1,
                time: this.data.time / 1e3,
                score: e,
                ranking: "",
                target: this.data.seconds
            }), this.setData({
                running: !1,
                players: a
            }), this.sortByScore(), this.sortByName();
        }
    },
    sortByScore: function() {
        var t = this.data.players;
        t.sort(function(t, a) {
            return Math.abs(t.score) - Math.abs(a.score);
        }), t.forEach(function(t, a) {
            t.ranking = a + 1;
        }), this.setData({
            players: t
        });
    },
    sortByName: function() {
        var t = this.data.players;
        t.sort(function(t, a) {
            return t.name - a.name;
        }), this.setData({
            players: t
        });
    },
    onHide: function() {
        this.end();
    },
    onShareAppMessage: function() {
        return {
            title: "掐点 - 时间刚刚好，谁是掐点大王"
        };
    }
});